package com.youease.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NpcTranslate {
	
	public static String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<npcs>";
	public static Map<Integer,String> map = new HashMap<Integer,String>();
	
	public static String directory = "f:\\";
	
	public static void main(String[] args) {
		analyzeXls();
		generateXml();
	}
	
	public static void analyzeXls() {
		Workbook book;
		try {
			book = Workbook.getWorkbook(new File(directory + "npc_position.xls"));
			Sheet sheet = book.getSheet(0);
			for(int i=0;i<sheet.getRows();i++) {
				if(i!=0)
					xml += "\n\t<npc>";
				for(int j=0;j<sheet.getColumns();j++) {
					if(i==0) {
						map.put(j,sheet.getCell(j, i).getContents().trim());
					}else {
						String content = sheet.getCell(j,i).getContents();
//						content = "<![CDATA["+content+"]]>";
						String column = map.get(j);
						if("nKind".equals(column)) {
							xml +="\n\t\t<nKind>"+content+"</nKind>";
						}else if("strName".equals(column)) {
							xml +="\n\t\t<strName>"+"<![CDATA["+content+"]]>"+"</strName>";
						}else if("xPos".equals(column)){
							xml +="\n\t\t<xPos>"+content+"</xPos>";
						}else if("yPos".equals(column)){
							xml +="\n\t\t<yPos>"+content+"</yPos>";
						}else if("frameWidth".equals(column)){
							xml +="\n\t\t<frameWidth>"+content+"</frameWidth>";
						}else if("frameHeight".equals(column)){
							xml +="\n\t\t<frameHeight>"+content+"</frameHeight>";
						}else if("fps".equals(column)){
							xml +="\n\t\t<fps>"+content+"</fps>";
						}else if("players".equals(column)){
							xml +="\n\t\t<players>"+content+"</players>";
						}else if("frames".equals(column)) {
							xml +="\n\t\t<frames>"+content+"</frames>";
						}else if("open".equals(column)) {
							xml +="\n\t\t<open>"+content+"</open>";
						}
					}
				}
				if(i!=0) {
					xml += "\n\t</npc>";
				}
			}
			xml += "\n</npcs>";
			System.out.println(xml);
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void generateXml() {
		BufferedWriter buffereWriter = null;
		try {
			OutputStreamWriter fileWrite = new OutputStreamWriter(new FileOutputStream(new File(directory+"npc_position.xml")), "UTF-8");
            buffereWriter = new BufferedWriter(fileWrite);
//			fw = new FileWriter(new File("e:\\change\\home\\help_home.xml"));
            buffereWriter.write(xml);
            buffereWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(buffereWriter!=null) {
				try {
					buffereWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
